package com.liwq.controller;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.rpc.service.GenericService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GenericController {

    @RequestMapping("sayHello")
    public String sayHello(String a){
        ApplicationConfig application = new ApplicationConfig();
        application.setName("api-generic-consumer");
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("redis://localhost:6379");
        application.setRegistry(registry);

        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        // 弱类型接口名
        reference.setInterface("com.ynet.user.service.UserServiceMy");
        // 声明为泛化接口
        reference.setGeneric(true);
        reference.setApplication(application);

        // 用com.apache.dubbo.rpc.service.GenericService可以替代所有接口引用
        GenericService genericService = reference.get();

        Object name = genericService.$invoke("sayHello", new String[]{String.class.getName()}, new Object[]{ a });
        return name.toString();
    }

}
